/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_TEXTURE2D_IMPL__
#define __CC_PYTHON_IMPORT_O_TEXTURE2D_IMPL__

#include "python_object_texture2d.h"
#include "python_object_image.h"

NS_CCPY_BEGIN
	PYCLASS_INIT_AND_BASE(CPyTexture2D, CPyCCObject, "CCTexture2D", "Python CCTexture2D Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyTexture2D)
		PYCLASS_ADD_METHOD(CPyTexture2D, releaseData)
		PYCLASS_ADD_METHOD(CPyTexture2D, keepData)
		PYCLASS_ADD_METHOD(CPyTexture2D, initWithData)
		PYCLASS_ADD_METHOD(CPyTexture2D, drawAtPoint)
		PYCLASS_ADD_METHOD(CPyTexture2D, drawInRect)
		PYCLASS_ADD_METHOD(CPyTexture2D, initWithImage)
		PYCLASS_ADD_METHOD(CPyTexture2D, initWithPVRFile)
		PYCLASS_ADD_METHOD(CPyTexture2D, setTexParameters)
		PYCLASS_ADD_METHOD(CPyTexture2D, setAntiAliasTexParameters)
		PYCLASS_ADD_METHOD(CPyTexture2D, setAliasTexParameters)
		PYCLASS_ADD_METHOD(CPyTexture2D, generateMipmap)
		PYCLASS_ADD_METHOD(CPyTexture2D, stringForFormat)
		PYCLASS_ADD_METHOD(CPyTexture2D, bitsPerPixelForFormat)
		//PYCLASS_METHOD_DEF(CPyTexture2D, bitsPerPixelForFormat);
		PYCLASS_ADD_METHOD(CPyTexture2D, getContentSizeInPixels)
		PYCLASS_ADD_METHOD_EX(CPyTexture2D, initWithString					, METH_KEYWORDS|METH_VARARGS)  
		//static
		PYCLASS_ADD_METHOD_EX(CPyTexture2D, setDefaultAlphaPixelFormat		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTexture2D, defaultAlphaPixelFormat			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTexture2D, PVRImagesHavePremultipliedAlpha	, METH_STATIC|METH_VARARGS)
	PYCLASS_END_METHOD_DEF(CPyTexture2D)
	PYCLASS_BEGIN_GETSET_DEF(CPyTexture2D)
		PYCLASS_ADD_GET(CPyTexture2D, hasPremultipliedAlpha)
		PYCLASS_ADD_GET(CPyTexture2D, hasMipmaps)
	PYCLASS_END_GETSET_DEF(CPyTexture2D)
	PYCLASS_IMP_INTERFACE_NEW(CPyTexture2D)

	int CPyTexture2D::__init__(CPyTexture2D *pySelf, PyObject *args, PyObject *kwds)
	{
		pySelf->setRaw();
		return 0;
	}

	void CPyTexture2D::__del__(CPyTexture2D *self)
	{
		self->ob_type->tp_free(self);
	}
	
	PYCLASS_METHOD_IMPL(CPyTexture2D, releaseData)
	{
		CPyPointer *ptr = NULL;
		FROM_PYPARAM_TO_CPTR(ptr);
		if(!ptr)
			Py_RETURN_NONE;
		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, releaseData, ptr->getPtr());
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, keepData)
	{
		CPyPointer *ptr = NULL;
		FROM_PYPARAM_TO_CPTR(ptr);
		if(!ptr)
			Py_RETURN_NONE;
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, keepData, void *, ptr->getPtr(), ptr->getSize());
		return (PyObject *)ptr;
	}
	
	PYCLASS_METHOD_IMPL(CPyTexture2D, initWithData)
	{
		PyObject *pptr = NULL;
		PyObject *psize = NULL;
		int npixelFormat = cocos2d::kCCTexture2DPixelFormat_RGBA8888;
		unsigned int w = 0, h = 0;

		if( !PyArg_ParseTuple(pyArgs, "OiIIO", &pptr, &npixelFormat, &w, &h, &psize) )
			Py_RETURN_NONE;

		if(!PyObject_TypeCheck(pptr, CPyClassBase::GetTypeObject<CPyPointer>()))
			Py_RETURN_NONE;
		
		if(!PyObject_TypeCheck(psize, CPyClassBase::GetTypeObject<CPySize>()))
			Py_RETURN_NONE;

		CPyPointer *ptr = (CPyPointer *)pptr;
		CPySize *size = (CPySize *)psize;
		cocos2d::CCTexture2DPixelFormat pixelFormat = static_cast<cocos2d::CCTexture2DPixelFormat>(npixelFormat);
		
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, initWithData, bool, ptr->getPtr(), pixelFormat, w, h, size->getCCObject());
		Py_RETURN_BOOL(v);
	}
	
	PYCLASS_METHOD_IMPL(CPyTexture2D, drawAtPoint)
	{
		PyObject *ppt = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &ppt) )
			Py_RETURN_NONE;

		if(!PyObject_TypeCheck(ppt, CPyClassBase::GetTypeObject<CPyPoint>()))
			Py_RETURN_NONE;

		CPyPoint *pt = (CPyPoint *)ppt;

		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, drawAtPoint, pt->getCCObject());
		Py_RETURN_NONE;
	}
	
	PYCLASS_METHOD_IMPL(CPyTexture2D, drawInRect)
	{
		PyObject *prt = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &prt) )
			Py_RETURN_NONE;

		if(!PyObject_TypeCheck(prt, CPyClassBase::GetTypeObject<CPyRect>()))
			Py_RETURN_NONE;

		CPyRect *rt = (CPyRect *)prt;

		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, drawInRect, rt->getCCObject());
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, initWithImage)
	{
		PyObject *pimage = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &pimage) )
			Py_RETURN_NONE;

		if(!PyObject_TypeCheck(pimage, CPyClassBase::GetTypeObject<CPyImage>()))
			Py_RETURN_NONE;

		CPyImage *image = (CPyImage *)pimage;

		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, initWithImage, dynamic_cast<cocos2d::CCImage *>(image->getCCObject()));
		Py_RETURN_NONE;
	}
	
	PYCLASS_METHOD_KW_IMPL(CPyTexture2D, initWithString)
	{
		if( !pyArgs )
			Py_RETURN_FALSE;
		static char* kwlist[] = {"text", "fontName", "fontSize", "dimensions", "halign", "valign", NULL};
		char *text = "";
		char *fontname = "";
		int s = 0;
		PyObject *pdimensions = NULL;
		int hAlignment = 0;
		int vAlignment = 0;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"ssi|Oii",kwlist, &text, &fontname, &s, &pdimensions, &hAlignment, &vAlignment))
			Py_RETURN_FALSE;
		if(!text || strlen(text) == 0)
			Py_RETURN_FALSE;
		
		if(!PyObject_TypeCheck(pdimensions, CPyClassBase::GetTypeObject<CPySize>()))
			Py_RETURN_NONE;

		CPySize *dimensions = (CPySize *)pdimensions;
		
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, initWithString, bool, text, fontname, s, dimensions->getCCObject()
			,static_cast<cocos2d::CCTextAlignment>(hAlignment)
			,static_cast<cocos2d::CCVerticalTextAlignment>(vAlignment)
			);
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, initWithPVRFile)
	{
		char *path = "";
		if( !PyArg_ParseTuple(pyArgs, "s", &path) )
			Py_RETURN_NONE;

		if(!path || strlen(path) == 0)
			Py_RETURN_NONE;


		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, initWithPVRFile, bool, path);
		
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, setTexParameters)
	{
		PyObject *ppt = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &ppt) )
			Py_RETURN_NONE;

		if(!PyObject_TypeCheck(ppt, CPyClassBase::GetTypeObject<CPyTexParams>()))
			Py_RETURN_NONE;

		CPyTexParams *pt = (CPyTexParams *)ppt;

		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, setTexParameters, &(pt->getCCObject()));
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, setAntiAliasTexParameters)
	{
		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, setAntiAliasTexParameters);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, setAliasTexParameters)
	{
		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, setAliasTexParameters);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, generateMipmap)
	{
		CC_METHOD_WRAPPER_SETER(cocos2d::CCTexture2D *, CPyTexture2D, generateMipmap);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, stringForFormat)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, stringForFormat, const char *);
		return Py_BuildValue("s", v);
	}

	PYCLASS_METHOD_KW_IMPL(CPyTexture2D, bitsPerPixelForFormat)
	{
		if( !pyArgs )
			Py_RETURN_NONE;
		static char* kwlist[] = {"format", NULL};
		int format = -1;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"|i",kwlist, &format))
			Py_RETURN_NONE;
		if(format < 0)
		{
			CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, bitsPerPixelForFormat, unsigned int);
			return Py_BuildValue("I", v);
		}else
		{
			CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, bitsPerPixelForFormat, unsigned int, static_cast<cocos2d::CCTexture2DPixelFormat>(format));
			return Py_BuildValue("I", v);
		}
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, getContentSizeInPixels)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, getContentSizeInPixels, cocos2d::CCSize);
		CPySize *size = CPySize::create(v);
		return (PyObject *)size;
	}

	PYCLASS_GET_IMPL(CPyTexture2D, hasPremultipliedAlpha)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, hasPremultipliedAlpha, bool);
		Py_RETURN_BOOL(v);
	}

	PYCLASS_GET_IMPL(CPyTexture2D, hasMipmaps)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCTexture2D *, CPyTexture2D, hasMipmaps, bool);
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, setDefaultAlphaPixelFormat)
	{
		int format = -1;
		if( !PyArg_ParseTuple(pyArgs, "i", &format) )
			Py_RETURN_NONE;
		if(format < 0)
			Py_RETURN_NONE;
		cocos2d::CCTexture2D::setDefaultAlphaPixelFormat(static_cast<cocos2d::CCTexture2DPixelFormat>(format));
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, defaultAlphaPixelFormat)
	{
		return Py_BuildValue("I", cocos2d::CCTexture2D::defaultAlphaPixelFormat());
	}

	PYCLASS_METHOD_IMPL(CPyTexture2D, PVRImagesHavePremultipliedAlpha)
	{
		PyObject *pb = Py_False;
		if( !PyArg_ParseTuple(pyArgs, "O", &pb) )
			Py_RETURN_NONE;
		if(!pb)
			Py_RETURN_NONE;
		cocos2d::CCTexture2D::PVRImagesHavePremultipliedAlpha(pb != Py_False);
		Py_RETURN_NONE;
	}

NS_CCPY_END

#endif